package com.pet.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pet.model.common.dtos.ResponseResult;
import com.pet.model.common.enums.AppHttpCodeEnum;
import com.pet.model.media.dtos.WmMaterialDto;
import com.pet.model.media.pojos.WmFolders;
import com.pet.model.media.pojos.WmUser;
import com.pet.utils.threadlocal.WmThreadLocalUtils;
import com.pet.wemedia.service.WmFoldersService;
import com.pet.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/folders")
public class WmFolderController{

    @Resource
    private WmFoldersService folderService;
    @Autowired
    private WmMaterialService wmMaterialService;

    @PostMapping("/create")
    public ResponseResult<WmFolders> createFolder(@RequestBody WmFolders folder) {
        WmUser user = WmThreadLocalUtils.getUser();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 校验参数
        if (folder.getName() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "请填写文件夹名称");
        }
        // 设置创建时间
        folder.setCreatedTime(new Date());
        folder.setUserId(user.getId());
        // 调用业务层保存文件夹
        boolean success = folderService.save(folder);
        if (success) {
            return ResponseResult.okResult(folder);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹创建失败");
        }
    }


    @DeleteMapping("/delete")
    public ResponseResult<Void> deleteFolder(@RequestParam String id) {
        // 校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数缺失");
        }
        // 调用业务层删除文件夹
        boolean success = folderService.removeById(Integer.valueOf(id));
        if (success) {
            return ResponseResult.okResult(null);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹删除失败");
        }
    }

    @PutMapping("/move")
    public ResponseResult<WmFolders> moveFolder(@RequestParam String id, @RequestParam(required = false) String parentId) {
        // 校验参数
        if (id == null || parentId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数缺失");
        }
        // 获取文件夹对象
        WmFolders folder = folderService.getById(Integer.valueOf(id));
        if (folder == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹不存在");
        }
        // 更新父级id
        folder.setParentId(Integer.valueOf(parentId));
        // 调用业务层更新文件夹
        boolean success = folderService.updateById(folder);
        if (success) {
            return ResponseResult.okResult(folder);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹移动失败");
        }
    }

    @PutMapping("/rename")
    public ResponseResult<WmFolders> renameFolder(@RequestParam String id, @RequestParam String newName) {
        // 校验参数
        if (id == null || newName == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数缺失");
        }
        // 获取文件夹对象
        WmFolders folder = folderService.getById(Integer.valueOf(id));
        if (folder == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹不存在");
        }
        // 更新文件夹名称
        folder.setName(newName);
        // 调用业务层更新文件夹
        boolean success = folderService.updateById(folder);
        if (success) {
            return ResponseResult.okResult(folder);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件夹重命名失败");
        }
    }

    @GetMapping("/list")
    public ResponseResult<List<WmFolders>> listFolders(@RequestParam(required = false) Integer userId) {

        // 调用业务层查询文件夹列表
        List<WmFolders> folderList = folderService.list(userId);
        return ResponseResult.okResult(folderList);
    }

    @GetMapping("/get")
    public ResponseResult<WmFolders> getFolders(@RequestParam(required = false) Integer id) {

        return ResponseResult.okResult(folderService.getById(id));
    }

    @GetMapping("/get/{name}")
    public ResponseResult<WmFolders> getNameFolders(@PathVariable String name) {
        QueryWrapper<WmFolders> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.eq("name", name);
        }
        return ResponseResult.okResult(folderService.getNameFolders(queryWrapper));
    }

    @PostMapping("/material/list")
    public ResponseResult findList(@RequestBody WmMaterialDto dto) {
        return wmMaterialService.byFidList(dto);
    }
}